package com.klikmee.sms.core.base.postman;

import java.net.URL;

import javax.xml.ws.BindingProvider;

import com.klikmee.sms.cef.services.ws.ServiceInterface;
import com.klikmee.sms.cef.services.ws.ServiceInterfaceService;
import com.klikmee.sms.core.commons.events.Dispatcher;
import com.klikmee.sms.core.commons.queues.QueueItem;
import com.klikmee.sms.core.commons.queues.QueueItemProcessor;

public abstract class ItemProcessorBase<T> implements QueueItemProcessor<T> {

	/**
	 * URL of the WSDL file for the ws that every sms service instance
	 * implements to communicate to core platform
	 */
	protected URL				wsdlURL			= null;

	/**
	 * Stub through which to communicate to service
	 */
	protected ServiceInterface	serviceProxy	= null;

	/**
	 * Dispatcher for delivery events
	 */
	protected Dispatcher		dispatcher;

	public ItemProcessorBase() {
		super();
	}

	/**
	 * This method initializes message deliverer.
	 * 
	 */
	public void init() {
		// instantiate sms service ws client stub
		serviceProxy = new ServiceInterfaceService( this.wsdlURL ).getServiceInterfacePort();
	}

	@Override
	public abstract boolean process(QueueItem<T> item);

	/**
	 * Overrides web service endpoint address
	 * 
	 * @param newEndpointAddress
	 */
	protected void overrideEndpointAddress(String newEndpointAddress) {
		BindingProvider provider = (BindingProvider) serviceProxy;
		provider.getRequestContext().put( BindingProvider.ENDPOINT_ADDRESS_PROPERTY, newEndpointAddress );
	}

	public URL getWsdlURL() {
		return wsdlURL;
	}

	public void setWsdlURL(URL wsdlURL) {
		this.wsdlURL = wsdlURL;
	}

	public ServiceInterface getServiceProxy() {
		return serviceProxy;
	}

	public void setServiceProxy(ServiceInterface serviceProxy) {
		this.serviceProxy = serviceProxy;
	}
	
	public void setDispatcher(Dispatcher dispatcher) {
		this.dispatcher = dispatcher;
	}

}